package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.backend.Dish;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/dish")
public class DishContriller {

    @Autowired
    private DishService dishService;


    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R select(Integer page, Integer pageSize, String name) {
        return dishService.select(page, pageSize, name);
    }


    /**
     * 按id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectByid(@PathVariable String id) {
        Dish dish = dishService.selectByid(id);
//        System.out.println(dish);
        if (dish != null) {
            return R.success(dish);
        } else {
            return R.error("暂无菜品");
        }
    }


    /**
     * 添加
     *
     * @param request
     * @param dish
     * @return
     */
    @PostMapping
    public R add(HttpServletRequest request, @RequestBody Dish dish) {
        return dishService.add(request, dish);
    }


    /**
     * 菜品下拉列表
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R dishlist(String categoryId) {
        return dishService.dishlist(categoryId);
    }


    @PutMapping
    public R updatDish(HttpServletRequest request,@RequestBody Dish dish) {
        boolean b = dishService.updatDish(request, dish);
        if (b){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }


    /**
     * 删除
     */
    @DeleteMapping
    public R deleteDish(String[] ids) {
        return dishService.deletdish(ids);
    }

    /**
     * 修改售卖状态
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(String[] ids, @PathVariable Integer status) {
        return dishService.status(ids,status);
    }

}
